package com.fyzk.activity.welcome;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.fyzk.activity.main.MainGroupActivityI;
import com.fyzk.activity.user.LoginActivity;
import com.fyzk.base.BaseActivity;
import com.fyzk.data.preference.MySharedPreferences;
import com.fyzk.net.Commands;
import com.fyzk.utils.AppUtil;
import com.tyjc.lottery.R;

import org.json.JSONObject;

import http.Common.CommandUtil;
import http.StringResultBean;

/**
 * Created by fanxs11 on 17/6/12.
 */
public class WelcomeActivity extends BaseActivity implements View.OnClickListener {
    private Context context = this;
    private ImageView ivSplashPic;
    private Button btnSkip;
    private String curVersion;
    private String oldVersion;
    private String splashUrl;
    private CheckCodeCountTimer mCountDownTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome_layout2);
        mImmersionBar.fitsSystemWindows(false).reset().init();
        curVersion = AppUtil.getCurrentVersion(context);
        oldVersion = MySharedPreferences.getInstance().getSaveVersion();
//        splashUrl = MySharedPreferences.getInstance().getSplashImageUrl();
        splashUrl = "";
        initView();
        requestGoucaiStatus();
    }

    public void initView() {
        ivSplashPic = findViewById(R.id.splashUI_ivSplashPic);
        btnSkip = findViewById(R.id.splashUI_btnSkip);
        btnSkip.setOnClickListener(this);
        if (!TextUtils.isEmpty(splashUrl)) {
            Glide.with(context).load(splashUrl)
                    .placeholder(R.drawable.welcome_bg)
                    .error(R.drawable.welcome_bg)
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .into(ivSplashPic);
        }
        mCountDownTimer = new CheckCodeCountTimer(1000, 1000);
        mCountDownTimer.start();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.splashUI_btnSkip:
                comein();
                break;
        }
    }

    public class CheckCodeCountTimer extends CountDownTimer {

        public CheckCodeCountTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            btnSkip.setText("跳过 " + millisUntilFinished / 1000);
        }

        @Override
        public void onFinish() {
            comein();
        }
    }

    private void requestGoucaiStatus() {
        JSONObject json = new JSONObject();
        httpPostJson(CommandUtil.CHANNELSALE, Commands.CHANNELSALE, json, false);
    }

    private void comein() {
//        if (MySharedPreferences.getInstance().isFirst() || !curVersion.equals(oldVersion)) {
//            //将登录标志位设置为false，下次登录时不在显示首次登录界面
//            MySharedPreferences.getInstance().setSaveVersion(curVersion);
//            Intent intent = new Intent(context, AndyViewPagerActivity.class);
//            startActivity(intent);
//            finish();
//        } else {
            if (!MySharedPreferences.getInstance().getLoginStatus()) {
                Intent intent = new Intent(context, LoginActivity.class);
                startActivity(intent);
                finish();
            }else{
                Intent intent = new Intent(context, MainGroupActivityI.class);
                startActivity(intent);
                finish();
            }
//        }
    }

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        try {
            boolean isopen = Boolean.parseBoolean(resultBean.getData());
            MySharedPreferences.getInstance().setOpenStatus(isopen);
        } catch (RuntimeException e) {
        }
    }

    protected void onFailure(int requestCode, String errorCode, String message) {
        super.onFailure(requestCode, errorCode, message);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mHandler) {
            mHandler.removeCallbacksAndMessages(null);
        }
        if (null != mCountDownTimer) {
            mCountDownTimer.cancel();
            mCountDownTimer = null;
        }
    }
}
